<?php

/**
* Script for combining and compressing CSS.
*/

// Allow easy console colloring.
require_once 'Console/Color.php';
 
// Initialize the application path and autoloading.
$dirname = $_SERVER['PWD'];

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath($dirname . '/../www/application'));

set_include_path(implode(PATH_SEPARATOR, array(
    APPLICATION_PATH . '/../www/library',
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
Zend_Session::Start();
 
// Define some CLI options.
$getopt = new Zend_Console_Getopt(array(
   'env|e-s' => 'Application environment for which to create model s(defaults to development)',
   'help|h'  => 'Help -- usage message',
));

try
{
   $getopt->parse();
}
catch (Zend_Console_Getopt_Exception $e)
{
   // Bad options passed: report usage.
   echo $e->getUsageMessage();
   return false;
}
 
// If help requested, report usage message.
if($getopt->getOption('h'))
{
   echo $getopt->getUsageMessage();
   return true;
}
 
// Initialize values based on presence or absence of CLI options.
$env      = $getopt->getOption('e');
if($env === null)
   $env = get_current_user();

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (null === $env) ? 'development' : $env);
 
echo 'Using APPLICATION_ENV = ' . APPLICATION_ENV . PHP_EOL;

$tableName = $getopt->getOption('t');

// Process each CSS file first.
foreach(listdir(APPLICATION_PATH . '/../public/css') as $filename)
{
   // put the content into the file
   $pathinfo = pathinfo($filename);
   if(!isset($pathinfo["extension"]) || $pathinfo["extension"] != "css")
      continue;

   $filepath = APPLICATION_PATH . '/../public/css/' . $filename;

   if(file_exists('/wv/sdm/development/tools/yahoo/latest'))
      exec('java -jar /wv/sdm/development/tools/yahoo/latest ' . $filepath . ' -o ' . $filepath);
}

// Concat all files, but must be in correct order.
@mkdir(APPLICATION_PATH . '/../public/css/built');

// Order-specific files first.
$fp1 = fopen(APPLICATION_PATH . '/../public/css/built/01.css', 'w');
$order = array();
$orderFile = file_get_contents(APPLICATION_PATH . '/../public/css/' . 'order.txt');
if($orderFile !== FALSE)
{
   $order = explode("\n", $orderFile);
   foreach($order as $filename)
   {
      $filepath = APPLICATION_PATH . '/../public/css/' . $filename;
      $file2 = file_get_contents($filepath);
      fwrite($fp1, $file2);
   }
}

// All remaining files.
foreach(listdir(APPLICATION_PATH . '/../public/css') as $filename)
{
   if(in_array($filename, $order))
      continue;

   $pathinfo = pathinfo($filename);
   if(!isset($pathinfo["extension"]) || $pathinfo["extension"] != "css")
      continue;

   $filepath = APPLICATION_PATH . '/../public/css/' . $filename;
   $file2 = file_get_contents($filepath);
   fwrite($fp1, $file2);
}

fclose($fp1);

// http://php.net/manual/en/function.readdir.php
// post: frasq at frasq dot org 01-Nov-2010 03:46
function listdir($dir='.')
{ 
   if(!is_dir($dir))
      return false; 
    
   $files = array(); 
   listdiraux($dir, $files); 

   return $files; 
} 

function listdiraux($dir, &$files)
{ 
   $handle = opendir($dir); 
   while(($file = readdir($handle)) !== false)
   { 
      if($file == '.' || $file == '..')
         continue; 

      $filepath = $dir == '.' ? $file : $dir . '/' . $file; 
      $files[] = $file; 
   } 

   closedir($handle); 
} 
